package com.itheima.health.service.impl;

import com.itheima.health.dao.MemberDao;
import com.itheima.health.pojo.Member;
import com.itheima.health.service.MemberService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.*;

@Slf4j
@DubboService
public class MemberServiceImpl implements MemberService {

    @Autowired
    private MemberDao memberDao;

    @Override
    public Member findById(Integer id) {
        Member byIdMember = memberDao.selectByPrimaryKey(id);
        log.info("[会员-member-根据id查出数据] - member: {}", byIdMember);
        return byIdMember;
    }

    @Override
    public Member findByPhone(String phone) {
        Member member = new Member();
        member.setPhoneNumber(phone);
        Member byPhoneMember = memberDao.selectOne(member);
        log.info("[会员-member-根据phone查出数据] - phone: {}", byPhoneMember);
        return byPhoneMember;
    }

    @Override
    public Integer findByPhoneCount(String phone) {
        Member member = new Member();
        member.setPhoneNumber(phone);
        int count = memberDao.selectCount(member);
        log.info("[会员-member-根据phone查出count] - count: {}", count);
        return count;
    }

    @Transactional
    @Override
    public Member addMember(Member member) {
        /*
         * @Date: 11:53 上午 2021/1/12
         * @param: [member]
         * @return: void
         * @Description: 先判断是否有这个用户，如果有则更新没有则新增
         */
        Member newMember = new Member();
        newMember.setPhoneNumber(member.getPhoneNumber());
        Member selectMember = memberDao.selectOne(newMember);
        if (selectMember != null) {
            member.setId(selectMember.getId());
            memberDao.updateByPrimaryKeySelective(member);
        } else {
            member.setRegTime(new Date());
            memberDao.insertSelective(member);
        }
        return member;

    }

    @Override
    public List<Integer> countByMonth(List<String> monthList) {
        List<Integer> resultList = new ArrayList<>();
        for (String month : monthList) {
            String endDate = month+".31";
            Long count = memberDao.countByRegTimeBefore(endDate);
            resultList.add(count.intValue());
        }
        return resultList;
    }

    @Override
    public List<Integer> countByDate(List<String> months) {
        /*
         * @Date: 5:03 下午 2021/1/15
         * @name: countByDate
         * @param: [months]
         * @return: java.util.List<java.lang.Integer>
         * @Description: 可以判断31号～1号之间的数据，本次使用模糊匹配
         */
        List<Integer> memberCount = new ArrayList<>();
        try {
            for (String month : months) {
                Example example = new Example(Member.class);
                Example.Criteria criteria = example.createCriteria();
                criteria.andLike("regTime", "%" + month + "%");
                memberCount.add(memberDao.selectCountByExample(example));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return memberCount;
    }





    /**
     * 统计会员年龄占比饼图
     * 返回值格式：
     * [{"name":"xxx年龄     "，"value":人数}]
     *
     * @return
     */
    @Override
    public List<Integer> findMemberAgeCount() {

        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");

        // 查询所有会员
        List<Member> members = memberDao.selectAll();

        List<Integer> birAgeList = new ArrayList<>();
        for (Member member : members) {
            // 获取身份证号
            String idCard = member.getIdCard();
            // 调用根据身份证号获取年龄方法
            Integer birAge = getBirAgeSex(idCard);

            birAgeList.add(birAge);

        }

        return birAgeList;

    }




    /**
     * @Author: oooo
     * @Date: 10:15 2021/1/20
     * @Parms [certificateNo]
     * @ReturnType: java.lang.Integer
     * @Description: 根据身份证编号获取年龄
     */

    public Integer getBirAgeSex(String idCard) {
        // 首先定义一个接收年龄
        Integer age = null;
        // 获取现在的年份
        int year = Calendar.getInstance().get(Calendar.YEAR);
        // 转换为字符串数组
        char[] number = idCard.toCharArray();

        boolean flag = true;
        if (number.length == 15) {
            for (int x = 0; x < number.length; x++) {
                if (!flag)
                    return null;
                flag = Character.isDigit(number[x]);
            }
        } else if (number.length == 18) {
            for (int x = 0; x < number.length - 1; x++) {
                if (!flag)
                    return null;
                flag = Character.isDigit(number[x]);
            }
        }
        if (flag && idCard.length() == 15) {
            age = (year - Integer.parseInt("19" + idCard.substring(6, 8)));
        } else if (flag && idCard.length() == 18) {
            age = (year - Integer.parseInt(idCard.substring(6, 10)));
        }
        return age;

    }
}